<template>
	<view class="login">
		<u-popup v-model="show" mode="center" closeable='true'>
			<view class="popup">
				<view class="title">
					温馨提示
				</view>
				<view class="des">为了更好的为您服务，麻烦您授权</view>
				<u-button 
				class="btn" 
				shape="square" 
				size="medium" 
				open-type='getUserInfo' 
				@getuserinfo='getuser' 
				type="success">同步昵称头像</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false //弹窗
			}
		},
		onShow() {
			
		},
		onLoad() {

		},
		methods: {
			//判断用户是否授权
			authorize() {
				const _this = this
				wx.getSetting({
					success(res) {
					    if (res.authSetting['scope.userInfo']) {
							console.log('已授权', res)
							_this.show = false
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									console.log('用户信息：', infoRes.userInfo);
									uni.setStorage({
									    key: 'userInfo',
									    data: infoRes.userInfo,
									    success: function () {
									        console.log('success');
									    }
									});
									_this.login()
								}
							})
					    }else{
							console.log('未授权', res)
							_this.show = true
						}
					},
					fail(err) {
						console.log('未授权', err)
						_this.show = true
					}
				})
			},
			//登录
			login() {
				uni.checkSession({
					provider: 'weixin',
					success: function(res) {
						console.log('用户已登陆', res);
					},
					fail(err) {
						console.log('用户未登录', err)
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								console.log(loginRes);
								// 获取用户信息
			
							}
						});
					}
				});
			
			},
			//获取用户信息
			getuser(e) {
				console.log(e)
				if (e.detail.userInfo) {
					uni.setStorage({
					    key: 'userInfo',
					    data: e.detail.userInfo,
					    success: function () {
					        console.log('success');
					    }
					});
					this.show = false
					this.login()
				}
			
			}
		}
	}
</script>
<style lang="scss" scoped>
	.login {
		.popup {
			width: 80vw;
			padding: 0rpx 0 40rpx 0;
			text-align: center;

			.title {
				line-height: 100rpx;
				font-size: 16px;
				border-bottom: 1px solid #999;
			}

			.des {
				line-height: 100rpx;
			}
		}
	}
</style>
